#!/usr/bin/env ruby
require  'xmms'

def run_plugin(r)
	flagged = 0
	nexted = 0
	file = File.new("output","w+")
	message = String.new("")
	while(1)
	  	begin 
			if r.playlist? == true
				nexted = r.get_playlist_pos
				if (r.playing? == true and flagged == 0) then 	
					flagged = 1	
					nexted = r.get_playlist_pos
					print r.get_playlist_title,"\n"
					message << r.get_playlist_file << "\n"
					file.write(message)
					message = ""
				end
				if (r.playing? == false and r.paused? == false) then
					flagged = 0
				end
				if (nexted != r.get_playlist_pos) then 
					flagged = 0
				end
			end
		
		rescue Xmms::Error
			file.close
			break
		end
	end
end

def  start(r)
	while(1)
		if r.running? == true then
			run_plugin(r)
		end
	end
end



r = Xmms::Remote.new
start(r)
